<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>

        // var reg = /tmd|sb|md/gi;

        // var str = 'tmd 你真s是sB 真md牛逼';

        // console.log(str.replace(reg, '**'));


        function replaceWord(word) {
            // 敏感词数组
            var arr = ['tmd', 'md', 'sb', 'wc'];
            // 数组转字符串
            var str = arr.join('|');
            // 创建正则  str  是变量    new
            //   new RegExp(字符串 , 'ig')
            var reg = new RegExp(str, 'ig');
            console.log(reg);
            return word.replace(reg, '**');
        }

        var res = replaceWord('tmd 你真s是sB 真md牛逼')
        console.log(res);

    </script>

</body>

</html>